package com.david.reader.txt

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.david.reader.R
import com.david.reader.activity.ReaderActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class TxtTestActivity:Activity() {
    var dataBase:TxtDataBase? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_txt_test)
    }

    public fun createDataBase(view: View) {
        CoroutineScope(Dispatchers.IO).launch {
            dataBase =
            TxtDataBase.getTxtDataBase(this@TxtTestActivity, TxtDataBase.DATABASE_NAME)
        }

    }

    public fun insert(view: View) {
        CoroutineScope(Dispatchers.IO).launch {
            dataBase?.let {
                TxtUtils.getPages(
                    this@TxtTestActivity, it
                )
            }

//            dataBase?.txtPageDao()?.insertPage(
//            TxtPage(0, 0, "test pages")
//        )
        }
    }

    public fun query(view: View) {
        CoroutineScope(Dispatchers.IO).launch {

            val str = dataBase?.txtPageDao()?.getContentByPage(
                1
            )
            str?.content?.let { Log.e("content", it) }
        }
    }
    public fun reader(view: View) {
        startActivity(Intent(this, ReaderActivity::class.java))
    }
}